#include "monitor.h"
#include "memory/mem.h"
#include "device/device.h"
#include "cpu/difftest.h"

#include <getopt.h>
static char *img_file = NULL;
void sdb_set_batch_mode();
static int parse_args(int argc, char *argv[])
{
    const struct option table[] = {
        
    {"batch"    , no_argument      , NULL, 'b'},
    {"IMG"      , required_argument, NULL, 'I'},
    {"NONE"     , no_argument      , NULL, 'N'},
    {0, 0, NULL, 0},
    };
    int o;
    while ((o = getopt_long(argc, argv, "-bhl:d:p:", table, NULL)) != -1)
    {
        switch (o)
        {
        case 'b': sdb_set_batch_mode(); break;
        case 'N': break;
        case 1:
            img_file = optarg;
            return 0;
        default:
            printf("Usage: %s [OPTION...] IMAGE [args]\n\n", argv[0]);
            printf("\t-N,--NONE          none operation\n");
            printf("\n");
            exit(0);
        }
    }
    return 0;
}
void init_disasm(const char *triple);
void init_difftest(char *ref_so_file, long img_size, int port);
void init_regex();
void init_sdb();
void init_monitor(int argc, char *argv[])
{
    
    /* Parse arguments. */
    parse_args(argc, argv);

    /* Initialize memory. */
    init_mem();

    /* Initialize devices. */
    IFONE(CONFIG_DEVICE, init_device());

    /* Initialize disasm. */
    init_disasm("riscv64-pc-linux-gnu");
    
    
    long img_size = load_img(img_file);

    /* Initialize difftest. */
    char temp_str[128];
    sprintf(temp_str,"%s/build/riscv64-nemu-interpreter-so",getenv("NEMU_HOME"));
    init_difftest(temp_str, img_size, 1234);
    difftest_skip_ref();

    /* Initialize the simple debugger. */
    init_sdb();
}